<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>AMBHAS: /home/tomer/svn/ambhas/ambhas/copula.py Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AMBHAS
   
   </div>
   
  </td>
  
  
  
   
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('copula_8py.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">copula.py</div>  </div>
</div><!--header-->
<div class="contents">
<a href="copula_8py.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a><a class="code" href="namespaceambhas_1_1copula.html">00001</a> <span class="comment">#! /usr/bin/env python</span>
<a name="l00002"></a>00002 <span class="comment"># -*- coding: utf-8 -*-</span>
<a name="l00003"></a>00003 <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00004"></a>00004 <span class="stringliteral">Created on Wed Feb  9 19:13:28 2011</span>
<a name="l00005"></a>00005 <span class="stringliteral"></span>
<a name="l00006"></a>00006 <span class="stringliteral">@ author:                  Sat Kumar Tomer </span>
<a name="l00007"></a>00007 <span class="stringliteral">@ author&#39;s webpage:        http://civil.iisc.ernet.in/~satkumar/</span>
<a name="l00008"></a>00008 <span class="stringliteral">@ author&#39;s email id:       satkumartomer@gmail.com</span>
<a name="l00009"></a>00009 <span class="stringliteral">@ author&#39;s website:        www.ambhas.com</span>
<a name="l00010"></a>00010 <span class="stringliteral"></span>
<a name="l00011"></a>00011 <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00012"></a>00012 <span class="keyword">from</span> __future__ <span class="keyword">import</span> division
<a name="l00013"></a>00013 <span class="keyword">from</span> scipy.stats <span class="keyword">import</span> kendalltau, pearsonr, spearmanr
<a name="l00014"></a>00014 <span class="keyword">import</span> numpy <span class="keyword">as</span> np
<a name="l00015"></a>00015 <span class="keyword">from</span> scipy.integrate <span class="keyword">import</span> quad
<a name="l00016"></a>00016 <span class="keyword">from</span> scipy.optimize <span class="keyword">import</span> fmin
<a name="l00017"></a>00017 <span class="keyword">import</span> sys
<a name="l00018"></a>00018 <span class="keyword">import</span> statistics <span class="keyword">as</span> st
<a name="l00019"></a>00019 <span class="keyword">from</span> scipy.interpolate <span class="keyword">import</span> interp1d
<a name="l00020"></a>00020 <span class="keyword">from</span> stats <span class="keyword">import</span> scoreatpercentile
<a name="l00021"></a>00021 
<a name="l00022"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html">00022</a> <span class="keyword">class </span><a class="code" href="classambhas_1_1copula_1_1Copula.html">Copula</a>():
<a name="l00023"></a>00023     <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00024"></a>00024 <span class="stringliteral">    This class estimate parameter of copula</span>
<a name="l00025"></a>00025 <span class="stringliteral">    generate joint random variable for the parameters</span>
<a name="l00026"></a>00026 <span class="stringliteral">    This class has following three copulas:</span>
<a name="l00027"></a>00027 <span class="stringliteral">        Clayton</span>
<a name="l00028"></a>00028 <span class="stringliteral">        Frank</span>
<a name="l00029"></a>00029 <span class="stringliteral">        Gumbel</span>
<a name="l00030"></a>00030 <span class="stringliteral">        </span>
<a name="l00031"></a>00031 <span class="stringliteral">    Example:</span>
<a name="l00032"></a>00032 <span class="stringliteral">        x = np.random.normal(size=100)</span>
<a name="l00033"></a>00033 <span class="stringliteral">        y = np.random.normal(size=100)</span>
<a name="l00034"></a>00034 <span class="stringliteral">        foo = Copula(x, y, &#39;frank&#39;)</span>
<a name="l00035"></a>00035 <span class="stringliteral">        u,v = foo.generate(100)</span>
<a name="l00036"></a>00036 <span class="stringliteral">    &quot;&quot;&quot;</span>
<a name="l00037"></a>00037     
<a name="l00038"></a>00038 
<a name="l00039"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#a86ce10c313eecc377488649a2cdd2fe9">00039</a>     <span class="keyword">def </span><a class="code" href="classambhas_1_1copula_1_1Copula.html#a86ce10c313eecc377488649a2cdd2fe9">__init__</a>(self, X, Y, family):
<a name="l00040"></a>00040         <span class="stringliteral">&quot;&quot;&quot; initialise the class with X and Y</span>
<a name="l00041"></a>00041 <span class="stringliteral">        Input:</span>
<a name="l00042"></a>00042 <span class="stringliteral">            X:        one dimensional numpy array</span>
<a name="l00043"></a>00043 <span class="stringliteral">            Y:        one dimensional numpy array</span>
<a name="l00044"></a>00044 <span class="stringliteral">            family:   clayton or frank or gumbel</span>
<a name="l00045"></a>00045 <span class="stringliteral">            </span>
<a name="l00046"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#ada8379b8f501cee9f42f4ee0e8a2f59d">00046</a> <span class="stringliteral">            Note: the size of X and Y should be same</span>
<a name="l00047"></a>00047 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00048"></a>00048         <span class="comment"># check dimension of input arrays</span>
<a name="l00049"></a>00049         <span class="keywordflow">if</span> <span class="keywordflow">not</span> ((X.ndim==1) <span class="keywordflow">and</span> (Y.ndim==1)):
<a name="l00050"></a>00050             <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;The dimension of array should be one.&#39;</span>)
<a name="l00051"></a>00051         
<a name="l00052"></a>00052         <span class="comment"># input array should have same zie</span>
<a name="l00053"></a>00053         <span class="keywordflow">if</span> X.size <span class="keywordflow">is</span> <span class="keywordflow">not</span> Y.size:
<a name="l00054"></a>00054             <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;The size of both array should be same.&#39;</span>)
<a name="l00055"></a>00055         
<a name="l00056"></a>00056         <span class="comment"># check if the name of copula family correct</span>
<a name="l00057"></a>00057         copula_family = [<span class="stringliteral">&#39;clayton&#39;</span>, <span class="stringliteral">&#39;frank&#39;</span>, <span class="stringliteral">&#39;gumbel&#39;</span>]
<a name="l00058"></a>00058         <span class="keywordflow">if</span> family <span class="keywordflow">not</span> <span class="keywordflow">in</span> copula_family:
<a name="l00059"></a>00059             <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;The family should be clayton or frank or gumbel&#39;</span>)
<a name="l00060"></a>00060         
<a name="l00061"></a>00061         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a3bcd904534cfd1a416b3bc9a7e1c6e48">X</a> = X
<a name="l00062"></a>00062         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ada8379b8f501cee9f42f4ee0e8a2f59d">Y</a> = Y
<a name="l00063"></a>00063         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> = family
<a name="l00064"></a>00064         
<a name="l00065"></a>00065         <span class="comment"># estimate Kendall&#39;rank correlation</span>
<a name="l00066"></a>00066         tau = kendalltau(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a3bcd904534cfd1a416b3bc9a7e1c6e48">X</a>, self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ada8379b8f501cee9f42f4ee0e8a2f59d">Y</a>)[0]
<a name="l00067"></a>00067         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a216c9baeaf7e6565753364d99e3c3625">tau</a> = tau        
<a name="l00068"></a>00068         
<a name="l00069"></a>00069         <span class="comment"># estimate pearson R and spearman R</span>
<a name="l00070"></a>00070         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a97ff37542e40313a1027cbaae9df1dc5">pr</a> = pearsonr(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a3bcd904534cfd1a416b3bc9a7e1c6e48">X</a>, self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ada8379b8f501cee9f42f4ee0e8a2f59d">Y</a>)[0]
<a name="l00071"></a>00071         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a9b117c0351be05ae61802da857406eeb">sr</a> = spearmanr(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a3bcd904534cfd1a416b3bc9a7e1c6e48">X</a>, self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ada8379b8f501cee9f42f4ee0e8a2f59d">Y</a>)[0]
<a name="l00072"></a>00072         
<a name="l00073"></a>00073         <span class="comment"># estimate the parameter of copula</span>
<a name="l00074"></a>00074         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a7b90497c8d52923fba113fce635e672e">_get_parameter</a>()
<a name="l00075"></a>00075         
<a name="l00076"></a>00076         <span class="comment"># set U and V to none</span>
<a name="l00077"></a>00077         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8bbc3817979385df06c92750bcc35577">U</a> = <span class="keywordtype">None</span>
<a name="l00078"></a>00078         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ab0eb6e18841e81d2780e865f6c093b32">V</a> = <span class="keywordtype">None</span>
<a name="l00079"></a>00079         
<a name="l00080"></a>00080         
<a name="l00081"></a>00081     <span class="keyword">def </span>_get_parameter(self):
<a name="l00082"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">00082</a>         <span class="stringliteral">&quot;&quot;&quot; estimate the parameter (theta) of copula</span>
<a name="l00083"></a>00083 <span class="stringliteral">        &quot;&quot;&quot;</span>        
<a name="l00084"></a>00084         
<a name="l00085"></a>00085         <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> == <span class="stringliteral">&#39;clayton&#39;</span>:
<a name="l00086"></a>00086             self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> = 2*self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a216c9baeaf7e6565753364d99e3c3625">tau</a>/(1-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a216c9baeaf7e6565753364d99e3c3625">tau</a>)
<a name="l00087"></a>00087             
<a name="l00088"></a>00088         <span class="keywordflow">elif</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> == <span class="stringliteral">&#39;frank&#39;</span>:
<a name="l00089"></a>00089             self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> = -fmin(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a4034c1f2b6f52f9c8f35acc1e789ba62">_frank_fun</a>, -5, disp=<span class="keyword">False</span>)[0]
<a name="l00090"></a>00090             
<a name="l00091"></a>00091         <span class="keywordflow">elif</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> == <span class="stringliteral">&#39;gumbel&#39;</span>:
<a name="l00092"></a>00092             self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> = 1/(1-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a216c9baeaf7e6565753364d99e3c3625">tau</a>)
<a name="l00093"></a>00093     
<a name="l00094"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#ad22d02ed431702a07e931ae63684ae04">00094</a>     <span class="keyword">def </span><a class="code" href="classambhas_1_1copula_1_1Copula.html#ad22d02ed431702a07e931ae63684ae04">generate_uv</a>(self, n=1000):
<a name="l00095"></a>00095         <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00096"></a>00096 <span class="stringliteral">        Generate random variables (u,v)</span>
<a name="l00097"></a>00097 <span class="stringliteral">        </span>
<a name="l00098"></a>00098 <span class="stringliteral">        Input:</span>
<a name="l00099"></a>00099 <span class="stringliteral">            n:        number of random copula to be generated</span>
<a name="l00100"></a>00100 <span class="stringliteral">        </span>
<a name="l00101"></a>00101 <span class="stringliteral">        Output:</span>
<a name="l00102"></a>00102 <span class="stringliteral">            U and V:  generated copula</span>
<a name="l00103"></a>00103 <span class="stringliteral">            </span>
<a name="l00104"></a>00104 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00105"></a>00105         <span class="comment"># CLAYTON copula</span>
<a name="l00106"></a>00106         <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> == <span class="stringliteral">&#39;clayton&#39;</span>:
<a name="l00107"></a>00107             U = np.random.uniform(size = n)
<a name="l00108"></a>00108             W = np.random.uniform(size = n)
<a name="l00109"></a>00109                 
<a name="l00110"></a>00110             <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> &lt;= -1:
<a name="l00111"></a>00111                 <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;the parameter for clayton copula should be more than -1&#39;</span>)
<a name="l00112"></a>00112             <span class="keywordflow">elif</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>==0:
<a name="l00113"></a>00113                 <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;The parameter for clayton copula should not be 0&#39;</span>)
<a name="l00114"></a>00114                 
<a name="l00115"></a>00115             <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> &lt; sys.float_info.epsilon :
<a name="l00116"></a>00116                 V = W
<a name="l00117"></a>00117             <span class="keywordflow">else</span>:
<a name="l00118"></a>00118                 V = U*(W**(-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>/(1 + self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)) - 1 + U**self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)**(-1/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)
<a name="l00119"></a>00119     
<a name="l00120"></a>00120         <span class="comment"># FRANK copula</span>
<a name="l00121"></a>00121         <span class="keywordflow">elif</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> == <span class="stringliteral">&#39;frank&#39;</span>:
<a name="l00122"></a>00122             U = np.random.uniform(size = n)
<a name="l00123"></a>00123             W = np.random.uniform(size = n)
<a name="l00124"></a>00124             
<a name="l00125"></a>00125             <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> == 0:
<a name="l00126"></a>00126                 <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;The parameter for frank copula should not be 0&#39;</span>)
<a name="l00127"></a>00127             
<a name="l00128"></a>00128             <span class="keywordflow">if</span> abs(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>) &gt; np.log(sys.float_info.max):
<a name="l00129"></a>00129                 V = (U &lt; 0) + np.sign(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)*U
<a name="l00130"></a>00130             <span class="keywordflow">elif</span> abs(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>) &gt; np.sqrt(sys.float_info.epsilon):
<a name="l00131"></a>00131                 V = -np.log((np.exp(-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>*U)*(1-W)/W + np.exp(-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)
<a name="l00132"></a>00132                             )/(1 + np.exp(-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>*U)*(1-W)/W))/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>
<a name="l00133"></a>00133             <span class="keywordflow">else</span>:
<a name="l00134"></a>00134                 V = W
<a name="l00135"></a>00135         
<a name="l00136"></a>00136         <span class="comment"># GUMBEL copula</span>
<a name="l00137"></a>00137         <span class="keywordflow">elif</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a01095c08d6dbdb627ed09e5f7e4025cc">family</a> == <span class="stringliteral">&#39;gumbel&#39;</span>:
<a name="l00138"></a>00138             <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> &lt;= 1 :
<a name="l00139"></a>00139                 <span class="keywordflow">raise</span> ValueError(<span class="stringliteral">&#39;the parameter for GUMBEL copula should be greater than 1&#39;</span>)
<a name="l00140"></a>00140             <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a> &lt; 1 + sys.float_info.epsilon:
<a name="l00141"></a>00141                 U = np.random.uniform(size = n)
<a name="l00142"></a>00142                 V = np.random.uniform(size = n)
<a name="l00143"></a>00143             <span class="keywordflow">else</span>:
<a name="l00144"></a>00144                 u = np.random.uniform(size = n)
<a name="l00145"></a>00145                 w = np.random.uniform(size = n)
<a name="l00146"></a>00146                 w1 = np.random.uniform(size = n)
<a name="l00147"></a>00147                 w2 = np.random.uniform(size = n)
<a name="l00148"></a>00148                 
<a name="l00149"></a>00149                 u = (u - 0.5) * np.pi
<a name="l00150"></a>00150                 u2 = u + np.pi/2;
<a name="l00151"></a>00151                 e = -np.log(w)
<a name="l00152"></a>00152                 t = np.cos(u - u2/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)/ e
<a name="l00153"></a>00153                 gamma = (np.sin(u2/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)/t)**(1/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)*t/np.cos(u)
<a name="l00154"></a>00154                 s1 = (-np.log(w1))**(1/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)/gamma
<a name="l00155"></a>00155                 s2 = (-np.log(w2))**(1/self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a89c346b08a209a0debaaf28a9a07a35f">theta</a>)/gamma
<a name="l00156"></a>00156                 U = np.array(np.exp(-s1))
<a name="l00157"></a>00157                 V = np.array(np.exp(-s2))
<a name="l00158"></a>00158         
<a name="l00159"></a>00159         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8bbc3817979385df06c92750bcc35577">U</a> = U
<a name="l00160"></a>00160         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ab0eb6e18841e81d2780e865f6c093b32">V</a> = V        
<a name="l00161"></a>00161         <span class="keywordflow">return</span> U,V
<a name="l00162"></a>00162     
<a name="l00163"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#ac5f7d4eb2314db8480cb8908d3f9056d">00163</a>     <span class="keyword">def </span><a class="code" href="classambhas_1_1copula_1_1Copula.html#ac5f7d4eb2314db8480cb8908d3f9056d">generate_xy</a>(self, n=1000):
<a name="l00164"></a>00164         <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00165"></a>00165 <span class="stringliteral">        Generate random variables (x, y)</span>
<a name="l00166"></a>00166 <span class="stringliteral">        </span>
<a name="l00167"></a>00167 <span class="stringliteral">        Input:</span>
<a name="l00168"></a>00168 <span class="stringliteral">            n:        number of random copula to be generated</span>
<a name="l00169"></a>00169 <span class="stringliteral">        </span>
<a name="l00170"></a>00170 <span class="stringliteral">        Output:</span>
<a name="l00171"></a>00171 <span class="stringliteral">            X1 and Y1:  generated copula random numbers</span>
<a name="l00172"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#a8d8a3ad61052b19816d0180e24476f22">00172</a> <span class="stringliteral">            </span>
<a name="l00173"></a>00173 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00174"></a>00174         <span class="comment"># if U and V are not already generated</span>
<a name="l00175"></a>00175         <span class="keywordflow">if</span> self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8bbc3817979385df06c92750bcc35577">U</a> <span class="keywordflow">is</span> <span class="keywordtype">None</span>:
<a name="l00176"></a>00176             self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ad22d02ed431702a07e931ae63684ae04">generate_uv</a>(n)
<a name="l00177"></a>00177             
<a name="l00178"></a>00178         <span class="comment"># estimate inverse cdf of x andy</span>
<a name="l00179"></a>00179         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a463b24aac39703811e38966044a87d48">_inverse_cdf</a>()
<a name="l00180"></a>00180         
<a name="l00181"></a>00181         <span class="comment"># estimate X1 and Y1        </span>
<a name="l00182"></a>00182         X1 = self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ae5187cf74c0091df0969604d955cfed4">_inv_cdf_x</a>(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8bbc3817979385df06c92750bcc35577">U</a>)
<a name="l00183"></a>00183         Y1 = self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a06bb149235bb24f9883a65197bf56b47">_inv_cdf_y</a>(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ab0eb6e18841e81d2780e865f6c093b32">V</a>)
<a name="l00184"></a>00184         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a27c8f15259d9a0e93c0c3cffb6a41c1d">X1</a> = X1
<a name="l00185"></a>00185         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8d8a3ad61052b19816d0180e24476f22">Y1</a> = Y1
<a name="l00186"></a>00186         
<a name="l00187"></a>00187         <span class="keywordflow">return</span> X1, Y1
<a name="l00188"></a>00188 
<a name="l00189"></a><a class="code" href="classambhas_1_1copula_1_1Copula.html#a6df82e559634377f54c23d957d27144a">00189</a>     <span class="keyword">def </span><a class="code" href="classambhas_1_1copula_1_1Copula.html#a6df82e559634377f54c23d957d27144a">estimate</a>(self, data=None):
<a name="l00190"></a>00190         <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00191"></a>00191 <span class="stringliteral">        this function estimates the mean, std, iqr for the generated</span>
<a name="l00192"></a>00192 <span class="stringliteral">        ensemble</span>
<a name="l00193"></a>00193 <span class="stringliteral"></span>
<a name="l00194"></a>00194 <span class="stringliteral">        Output:</span>
<a name="l00195"></a>00195 <span class="stringliteral">            Y1_mean = mean of the simulated ensemble</span>
<a name="l00196"></a>00196 <span class="stringliteral">            Y1_std = std of the simulated ensemble</span>
<a name="l00197"></a>00197 <span class="stringliteral">            Y1_ll = lower limit of the simulated ensemble</span>
<a name="l00198"></a>00198 <span class="stringliteral">            Y1_ul = upper limit of the simulated ensemble</span>
<a name="l00199"></a>00199 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00200"></a>00200         nbin = 50
<a name="l00201"></a>00201         <span class="comment">#check if already the generate_xy has been called,</span>
<a name="l00202"></a>00202         <span class="comment">#if not called, call now</span>
<a name="l00203"></a>00203         <span class="keywordflow">try</span>:
<a name="l00204"></a>00204             self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a27c8f15259d9a0e93c0c3cffb6a41c1d">X1</a>
<a name="l00205"></a>00205             copula_ens = len(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a27c8f15259d9a0e93c0c3cffb6a41c1d">X1</a>)
<a name="l00206"></a>00206         <span class="keywordflow">except</span>:
<a name="l00207"></a>00207             copula_ens = 10000
<a name="l00208"></a>00208             self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ac5f7d4eb2314db8480cb8908d3f9056d">generate_xy</a>(copula_ens)
<a name="l00209"></a>00209         
<a name="l00210"></a>00210         <span class="keywordflow">if</span> data <span class="keywordflow">is</span> <span class="keywordtype">None</span>:
<a name="l00211"></a>00211             data = self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a3bcd904534cfd1a416b3bc9a7e1c6e48">X</a>
<a name="l00212"></a>00212         
<a name="l00213"></a>00213         n_ens = copula_ens/nbin <span class="comment">#average no. of bin in each class</span>
<a name="l00214"></a>00214         ind_sort = self.X1.argsort()
<a name="l00215"></a>00215         x_mean = np.zeros((nbin,))
<a name="l00216"></a>00216         y_mean = np.zeros((nbin,))
<a name="l00217"></a>00217         y_ul = np.zeros((nbin,))
<a name="l00218"></a>00218         y_ll = np.zeros((nbin,))
<a name="l00219"></a>00219         y_std = np.zeros((nbin,))
<a name="l00220"></a>00220     
<a name="l00221"></a>00221         <span class="keywordflow">for</span> ii <span class="keywordflow">in</span> range(nbin):
<a name="l00222"></a>00222             x_mean[ii] = self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a27c8f15259d9a0e93c0c3cffb6a41c1d">X1</a>[ind_sort[n_ens*ii:n_ens*(ii+1)]].mean()
<a name="l00223"></a>00223             y_mean[ii] = self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8d8a3ad61052b19816d0180e24476f22">Y1</a>[ind_sort[n_ens*ii:n_ens*(ii+1)]].mean()
<a name="l00224"></a>00224             y_std[ii] = self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8d8a3ad61052b19816d0180e24476f22">Y1</a>[ind_sort[n_ens*ii:n_ens*(ii+1)]].std()
<a name="l00225"></a>00225             y_ll[ii] = scoreatpercentile(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8d8a3ad61052b19816d0180e24476f22">Y1</a>[ind_sort[n_ens*ii:n_ens*(ii+1)]], 25)
<a name="l00226"></a>00226             y_ul[ii] = scoreatpercentile(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a8d8a3ad61052b19816d0180e24476f22">Y1</a>[ind_sort[n_ens*ii:n_ens*(ii+1)]], 75)
<a name="l00227"></a>00227             
<a name="l00228"></a>00228         foo_mean = interp1d(x_mean, y_mean, bounds_error=<span class="keyword">False</span>)
<a name="l00229"></a>00229         foo_std = interp1d(x_mean, y_std, bounds_error=<span class="keyword">False</span>)
<a name="l00230"></a>00230         foo_ll = interp1d(x_mean, y_ll, bounds_error=<span class="keyword">False</span>)
<a name="l00231"></a>00231         foo_ul = interp1d(x_mean, y_ul, bounds_error=<span class="keyword">False</span>)
<a name="l00232"></a>00232         
<a name="l00233"></a>00233         
<a name="l00234"></a>00234         Y1_mean = foo_mean(data)
<a name="l00235"></a>00235         Y1_std = foo_std(data)
<a name="l00236"></a>00236         Y1_ll = foo_ll(data)
<a name="l00237"></a>00237         Y1_ul = foo_ul(data)
<a name="l00238"></a>00238         
<a name="l00239"></a>00239         <span class="keywordflow">return</span> Y1_mean, Y1_std, Y1_ll, Y1_ul
<a name="l00240"></a>00240         
<a name="l00241"></a>00241    
<a name="l00242"></a>00242         
<a name="l00243"></a>00243     <span class="keyword">def </span>_inverse_cdf(self):
<a name="l00244"></a>00244         <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00245"></a>00245 <span class="stringliteral">        This module will calculate the inverse of CDF </span>
<a name="l00246"></a>00246 <span class="stringliteral">        which will be used in getting the ensemble of X and Y from</span>
<a name="l00247"></a>00247 <span class="stringliteral">        the ensemble of U and V</span>
<a name="l00248"></a>00248 <span class="stringliteral">        </span>
<a name="l00249"></a>00249 <span class="stringliteral">        The statistics module is used to estimate the CDF, which uses</span>
<a name="l00250"></a>00250 <span class="stringliteral">        kernel methold of cdf estimation</span>
<a name="l00251"></a>00251 <span class="stringliteral">        </span>
<a name="l00252"></a>00252 <span class="stringliteral">        To estimate the inverse of CDF, interpolation method is used, first cdf </span>
<a name="l00253"></a>00253 <span class="stringliteral">        is estimated at 100 points, now interpolation function is generated </span>
<a name="l00254"></a>00254 <span class="stringliteral">        to relate cdf at 100 points to data</span>
<a name="l00255"></a>00255 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00256"></a>00256         x2, x1 = st.cpdf(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a3bcd904534cfd1a416b3bc9a7e1c6e48">X</a>, kernel = <span class="stringliteral">&#39;Epanechnikov&#39;</span>, n = 100)
<a name="l00257"></a>00257         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ae5187cf74c0091df0969604d955cfed4">_inv_cdf_x</a> = interp1d(x2, x1)
<a name="l00258"></a>00258         
<a name="l00259"></a>00259         y2, y1 = st.cpdf(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ada8379b8f501cee9f42f4ee0e8a2f59d">Y</a>, kernel = <span class="stringliteral">&#39;Epanechnikov&#39;</span>, n = 100)
<a name="l00260"></a>00260         self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a06bb149235bb24f9883a65197bf56b47">_inv_cdf_y</a> = interp1d(y2, y1)
<a name="l00261"></a>00261         
<a name="l00262"></a>00262     
<a name="l00263"></a>00263     <span class="keyword">def </span>_integrand_debye(self,t):
<a name="l00264"></a>00264          <span class="stringliteral">&quot;&quot;&quot; </span>
<a name="l00265"></a>00265 <span class="stringliteral">         Integrand for the first order debye function</span>
<a name="l00266"></a>00266 <span class="stringliteral">         &quot;&quot;&quot;</span>
<a name="l00267"></a>00267          <span class="keywordflow">return</span> t/(np.exp(t)-1)
<a name="l00268"></a>00268          
<a name="l00269"></a>00269     <span class="keyword">def </span>_debye(self, alpha):
<a name="l00270"></a>00270         <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00271"></a>00271 <span class="stringliteral">        First order Debye function</span>
<a name="l00272"></a>00272 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00273"></a>00273         <span class="keywordflow">return</span> quad(self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a25dd6949457f8475c65770bfb0cb53af">_integrand_debye</a>, sys.float_info.epsilon, alpha)[0]/alpha
<a name="l00274"></a>00274     
<a name="l00275"></a>00275     <span class="keyword">def </span>_frank_fun(self, alpha):
<a name="l00276"></a>00276         <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00277"></a>00277 <span class="stringliteral">        optimization of this function will give the parameter for the frank copula</span>
<a name="l00278"></a>00278 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00279"></a>00279         diff = (1-self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#a216c9baeaf7e6565753364d99e3c3625">tau</a>)/4.0  - (self.<a class="code" href="classambhas_1_1copula_1_1Copula.html#ae7bc3c01f757396264b4d38cac26fdb5">_debye</a>(-alpha)-1)/alpha
<a name="l00280"></a>00280         <span class="keywordflow">return</span> diff**2
<a name="l00281"></a>00281         
<a name="l00282"></a>00282         
<a name="l00283"></a>00283 
<a name="l00284"></a>00284 
<a name="l00285"></a>00285 
<a name="l00286"></a>00286 
<a name="l00287"></a>00287 
<a name="l00288"></a>00288 
<a name="l00289"></a>00289 
<a name="l00290"></a>00290 
</pre></div></div><!-- contents -->
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="copula_8py.html">copula.py</a>      </li>

    <li class="footer">Generated on Sat Jul 21 2012 12:26:08 for AMBHAS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.6.1 </li>
   </ul>
 </div>


</body>
</html>
